#pragma once
#include <iostream>
#include <assert.h>

using namespace std;

namespace zzb 
{
	template<class T>
	struct list_node
	{
		list_node(const T& val = T())
			:_prev(nullptr)
			,_next(nullptr)
			, _val(val)
		{}
		list_node<T>* _prev;
		list_node<T>* _next;
		T _val;
	};
	template<class T, class Ref, class Ptr>
	struct __list_iteartor
	{
		typedef __list_iteartor<T, Ref, Ptr> iterator;
		typedef list_node<T> node;

		__list_iteartor(node* x)
			:_node(x)
		{
		}
		__list_iteartor(const iterator& it)
			:_node(it._node)																																				
		{
		}
		iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		iterator operator--(int)
		{
			iterator tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		iterator operator++(int)
		{
			iterator tmp(_node);
			_node++;
			return tmp;
		}
		Ref operator*()
		{
			return _node->_val;
		}
		bool operator!=(const iterator& it)
		{
			return _node != it._node;
		}
		bool operator==(const iterator& it)
		{
			return _node == it._node;
		}
		Ptr operator->()
		{
			return &(_node->_val);
		}
		node* _node;
	};

	template<class T>
	class list
	{
		void empty_construct()
		{
			node* head = new node;

			head->_next = head;
			head->_prev = head;
			_head = head;
		}
	public:
		typedef list_node<T> node;
		typedef __list_iteartor<T, T&, T*> iterator;
		typedef __list_iteartor<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		list()
		{
			empty_construct();
		}
		list(const list<T>& lt)
		{
			empty_construct();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		~list()
		{
			node* cur = _head->_next;
			while (cur != _head)
			{
				node* next = cur->_next;
				delete cur;
				cur = next;
			}
			delete _head;
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		bool empty()
		{
			return _head->_next == _head;
		}
		void push_front(const T& val)
		{
			/*node* new_node = new node(val);
			node* tail = _head->_prev;

			new_node->_prev = tail;
			tail->_next = new_node;
			new_node->_next = _head;
			_head->_prev = new_node;*/
			insert(begin(), val);
		}

		void pop_front()
		{
			/*assert(!empty());

			node* tail = _head->_prev;

			tail->_prev->_next = _head;
			_head->_prev = tail->_prev;
			delete tail;*/
			erase(begin());
		}
		size_t size()
		{
			size_t count = 0;
			for (auto& e : *this)
			{
				++count;
			}
			return count;
		}
		void push_back(const T& val)
		{
			/*node* new_node = new node(val);
			node* tail = _head->_prev;
			
			new_node->_prev = tail;
			tail->_next = new_node;
			new_node->_next = _head;
			_head->_prev = new_node;*/
			insert(iterator(_head), val);
		}
		
		void pop_back()
		{
			/*assert(!empty());

			node* tail = _head->_prev;

			tail->_prev->_next = _head;
			_head->_prev = tail->_prev;
			delete tail;*/
			erase(iterator(_head->_prev));
		}
		iterator insert(iterator pos, const T& val)
		{
			node* new_node = new node(val);
			new_node->_next = pos._node;
			new_node->_prev = pos._node->_prev;

			pos._node->_prev->_next = new_node;
			pos._node->_prev = new_node;
			return iterator(new_node);
		}
		iterator erase(iterator pos)
		{
			assert(!empty());
			iterator ret(pos);
			++ret;
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			delete pos._node;
			return ret;
		}
	private:
		node* _head;
	};

	void test_list1() 
	{
		list<int> lt;
		/*lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);*/
		lt.insert(lt.begin(), 1);
		lt.insert(lt.begin(), 2);
		lt.insert(lt.begin(), 3);
		lt.insert(lt.begin(), 4);
		lt.insert(lt.begin(), 5);


		auto it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << ' ';
		}
		cout << endl;
	}
	struct A
	{
		A(int a = 0, int b = 0)
			:_a(a)
			,_b(b)
		{
			
		}

		int _a;
		int _b;
	};
	void test_list2()
	{
		list<A> lt;
		lt.push_back(A(1, 2));
		lt.push_back(A(3, 4));
		lt.push_back(A(5, 6));
		lt.push_back(A(7, 8));

		list<A>::iterator it = lt.begin();

		while (it != lt.end())
		{
			//cout << it->_a << ' ' << it->_b << endl;
			cout << it->_a << ' ' << it->_b << endl;

			++it;
		}
	}
	void test_list3()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		for (auto e : lt)
		{
			cout << e << ' ';
		}
		cout << endl;
		while (!lt.empty())
		{
			lt.pop_back();
			for (auto e : lt)
			{
				cout << e << ' ';
			}
			cout << endl;
		}
	}
}